window.addEventListener('load', function () {

    Date.prototype.toLocaleString = function() {
        return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds() + "秒";
    };

    var trp = document.querySelector('.in-trp');
    var datevalue = document.querySelectorAll('.date');
    var timevalue = document.querySelectorAll('.time');
    var button = document.querySelector('.button');
    var arrlast = document.querySelector('.in-arr');
    var adptlast = document.querySelector('.in-adpt');
    function dateval() {
        var now = new Date();
        var day = ("0" + now.getDate()).slice(-2);
        var month = ("0" + (now.getMonth() + 1)).slice(-2);
        var today = now.getFullYear() + "-" + (month) + "-" + (day);
        return today;
    }
    console.log(datevalue);
    for (let i = 0; i < datevalue.length; i++) {
        datevalue[i].value = dateval();
    }
    trp.value = '04:00';
    timevalue[0].value = '18:00';
    timevalue[1].value = '20:00';
    timevalue[2].value = '21:00';
    arrlast.value = '03:00';
    adptlast.value = '02:00';
    button.addEventListener('click', function () {
        //数据date化
        var trpc = new Date('1970-1-1 ' + trp.value);
        var arrlastc = new Date('1970-1-1 ' + arrlast.value);
        var adptlastc = new Date('1970-1-1 ' + adptlast.value);
        // console.log(trp.value,trpc.valueOf());
        // 达到所需时间，离开所需时间
        var arr = trpc.valueOf() - arrlastc.valueOf();
        var adpt = trpc.valueOf() - adptlastc.valueOf();
        // 容错
        if(arr<0){
            arr=0;
        }
        if(adpt<0){
            adpt=0;
        }
        console.log(arr, adpt);

        var arrtime, adpttime;
        var tf = new Date(datevalue[0].value + ' ' + timevalue[0].value);
        var str = new Date(datevalue[1].value + ' ' + timevalue[1].value);
        var end = new Date(datevalue[2].value + ' ' + timevalue[2].value);
        arrtime = new Date(tf.valueOf() + arr);
        adpttime = new Date(tf.valueOf() + adpt);
        console.log(adpttime.valueOf()-str.valueOf(),arrtime.valueOf()-end.valueOf())
        var flag = true;
        if (adpttime.valueOf() - str.valueOf() > 0) {
            //需要提前
            var tflead = new Date(tf.valueOf() - adpttime.valueOf() + str.valueOf())
            
        } else{
            flag = false;
        }
        if (arrtime.valueOf() - end.valueOf() < 0) {
            //需要推迟
            var tfpf = new Date(tf.valueOf() - arrtime.valueOf() + end.valueOf())
        } else{
            
            flag = false;
        }
        if (flag == true) {
            var pos = parseInt(arr.valueOf() / (trpc.valueOf() + 28800000) * 1200);
            var len1 = parseInt((adpt.valueOf() - arr.valueOf()) / (trpc.valueOf() + 28800000) * 1200);
            console.log(arr.valueOf(), adpt.valueOf(), (trpc.valueOf() + 28800000), pos, len1);
            //禁航红线
            var prh=document.querySelector('.prh');
            console.log(prh);
            prh.style.left=pos+'px';
            prh.style.width=len1+'px';
            //剩余时间
            var arrdiv=document.querySelector('.arr');
            var adptdiv=document.querySelector('.adpt');
            var len2 = parseInt((arrlastc.valueOf() + 28800000) / (trpc.valueOf() + 28800000) * 1200);
            var len3 = parseInt((adptlastc.valueOf() + 28800000) / (trpc.valueOf() + 28800000) * 1200);
            console.log(len2,len3);
            arrdiv.style.width=len2+'px';
            adptdiv.style.width=len3+'px'
            //结果显示
            var lead=document.querySelector('.lead');
            var pf=document.querySelector('.pf');
            lead.style.color='red';
            pf.style.color='red';
            lead.innerHTML='起飞时间不宜晚于：' + tflead.toLocaleString();
            pf.innerHTML='起飞时间不宜早于：' + tfpf.toLocaleString();
            console.log('起飞时间不宜晚于：' + tflead + '起飞时间不宜早于:' + tfpf);
        } else {
            var lead=document.querySelector('.lead');
            lead.innerHTML='禁航不影响';
            console.log('禁航不影响');
        }



        // console.log(tf,str,end,arrtime, adpttime);
    })
    // console.log(date1);
});